home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 75UFCX (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  11.5 KB  |  446 lines

  1. package com.sun.java.swing.plaf.basic;
  2.  
  3. import com.sun.java.swing.JComponent;
  4. import com.sun.java.swing.JSplitPane;
  5. import com.sun.java.swing.LookAndFeel;
  6. import com.sun.java.swing.UIManager;
  7. import com.sun.java.swing.plaf.ComponentUI;
  8. import com.sun.java.swing.plaf.SplitPaneUI;
  9. import java.awt.Color;
  10. import java.awt.Component;
  11. import java.awt.Dimension;
  12. import java.awt.Graphics;
  13. import java.awt.Insets;
  14. import java.awt.LayoutManager;
  15. import java.awt.Rectangle;
  16. import java.awt.peer.ComponentPeer;
  17. import java.awt.peer.LightweightPeer;
  18. import java.beans.PropertyChangeEvent;
  19. import java.beans.PropertyChangeListener;
  20. import java.io.Serializable;
  21. import java.util.EventObject;
  22.  
  23. public class BasicSplitPaneUI extends SplitPaneUI implements PropertyChangeListener, Serializable {
  24.    protected static final String NON_CONTINUOUS_DIVIDER = "nonContinuousDivider";
  25.    protected JSplitPane splitPane;
  26.    private int orientation;
  27.    protected BasicHorizontalLayoutManager layoutManager;
  28.    private boolean continuousLayout;
  29.    protected BasicSplitPaneDivider divider;
  30.    private int lastDragLocation;
  31.    protected int dividerSize;
  32.    protected Component nonContinuousLayoutDivider;
  33.    protected boolean draggingHW;
  34.    protected int beginDragDividerLocation;
  35.  
  36.    public BasicSplitPaneUI() {
  37.       this.setLastDragLocation(-1);
  38.    }
  39.  
  40.    public BasicSplitPaneDivider createDefaultDivider() {
  41.       return new BasicSplitPaneDivider(this);
  42.    }
  43.  
  44.    protected Component createDefaultNonContinuousLayoutDivider() {
  45.       return new 1(this);
  46.    }
  47.  
  48.    public static ComponentUI createUI(JComponent x) {
  49.       return new BasicSplitPaneUI();
  50.    }
  51.  
  52.    protected void dragDividerTo(int location) {
  53.       if (this.getLastDragLocation() != location) {
  54.          if (this.isContinuousLayout()) {
  55.             Component leftC = this.splitPane.getLeftComponent();
  56.             Rectangle leftBounds = leftC.getBounds();
  57.             if (this.orientation == 1) {
  58.                leftC.setSize(location - leftBounds.x, leftBounds.height);
  59.             } else {
  60.                leftC.setSize(leftBounds.width, location - leftBounds.y);
  61.             }
  62.  
  63.             this.splitPane.validate();
  64.             this.splitPane.repaint();
  65.             this.setLastDragLocation(location);
  66.          } else {
  67.             int lastLoc = this.getLastDragLocation();
  68.             this.setLastDragLocation(location);
  69.             if (this.orientation == 1) {
  70.                int splitHeight = this.splitPane.getSize().height;
  71.                if (this.draggingHW) {
  72.                   this.nonContinuousLayoutDivider.setLocation(this.getLastDragLocation(), 0);
  73.                } else {
  74.                   this.splitPane.repaint(lastLoc, 0, this.dividerSize, splitHeight);
  75.                   this.splitPane.repaint(location, 0, this.dividerSize, splitHeight);
  76.                }
  77.             } else {
  78.                int splitWidth = this.splitPane.getSize().width;
  79.                if (this.draggingHW) {
  80.                   this.nonContinuousLayoutDivider.setLocation(0, this.getLastDragLocation());
  81.                } else {
  82.                   this.splitPane.repaint(0, lastLoc, splitWidth, this.dividerSize);
  83.                   this.splitPane.repaint(0, location, splitWidth, this.dividerSize);
  84.                }
  85.             }
  86.          }
  87.       }
  88.  
  89.    }
  90.  
  91.    protected void finishDraggingTo(int location) {
  92.       this.dragDividerTo(location);
  93.       this.setLastDragLocation(-1);
  94.       if (!this.isContinuousLayout()) {
  95.          Component leftC = this.splitPane.getLeftComponent();
  96.          Rectangle leftBounds = leftC.getBounds();
  97.          if (this.orientation == 1) {
  98.             int splitHeight = this.splitPane.getSize().height;
  99.             leftC.setSize(location - leftBounds.x, leftBounds.height);
  100.             if (this.draggingHW) {
  101.                this.nonContinuousLayoutDivider.setLocation(-this.dividerSize, 0);
  102.             }
  103.  
  104.             this.splitPane.paintImmediately(location, 0, this.dividerSize, splitHeight);
  105.          } else {
  106.             int splitWidth = this.splitPane.getSize().width;
  107.             leftC.setSize(leftBounds.width, location - leftBounds.y);
  108.             if (this.draggingHW) {
  109.                this.nonContinuousLayoutDivider.setLocation(0, -this.dividerSize);
  110.             }
  111.  
  112.             this.splitPane.paintImmediately(0, location, splitWidth, this.dividerSize);
  113.          }
  114.  
  115.          this.splitPane.validate();
  116.          this.splitPane.repaint();
  117.       }
  118.  
  119.       this.splitPane.setLastDividerLocation(this.beginDragDividerLocation);
  120.    }
  121.  
  122.    public void finishedPaintingChildren(JSplitPane jc, Graphics g) {
  123.       if (jc == this.splitPane && this.getLastDragLocation() != -1 && !this.isContinuousLayout() && !this.draggingHW) {
  124.          Dimension size = this.splitPane.getSize();
  125.          g.setColor(Color.darkGray);
  126.          if (this.orientation == 1) {
  127.             g.fillRect(this.getLastDragLocation(), 0, this.dividerSize - 1, size.height - 1);
  128.          } else {
  129.             g.fillRect(0, this.lastDragLocation, size.width - 1, this.dividerSize - 1);
  130.          }
  131.       }
  132.  
  133.    }
  134.  
  135.    public BasicSplitPaneDivider getDivider() {
  136.       return this.divider;
  137.    }
  138.  
  139.    protected int getDividerBorderSize() {
  140.       return 1;
  141.    }
  142.  
  143.    public int getDividerLocation() {
  144.       return this.orientation == 1 ? this.divider.getLocation().x : this.divider.getLocation().y;
  145.    }
  146.  
  147.    public Insets getInsets(JComponent jc) {
  148.       return null;
  149.    }
  150.  
  151.    public int getLastDragLocation() {
  152.       return this.lastDragLocation;
  153.    }
  154.  
  155.    public int getMaximumDividerLocation() {
  156.       Dimension splitPaneSize = this.splitPane.getSize();
  157.       int maxLoc = 0;
  158.       Component rightC = this.splitPane.getRightComponent();
  159.       if (rightC != null) {
  160.          Insets insets = this.splitPane.getInsets();
  161.          Dimension minSize = rightC.getMinimumSize();
  162.          if (this.orientation == 1) {
  163.             maxLoc = splitPaneSize.width - minSize.width;
  164.          } else {
  165.             maxLoc = splitPaneSize.height - minSize.height;
  166.          }
  167.  
  168.          maxLoc -= this.dividerSize + this.getDividerBorderSize();
  169.          if (insets != null) {
  170.             if (this.orientation == 1) {
  171.                maxLoc += insets.left;
  172.             } else {
  173.                maxLoc += insets.top;
  174.             }
  175.          }
  176.       }
  177.  
  178.       return Math.max(this.getMinimumDividerLocation(), maxLoc);
  179.    }
  180.  
  181.    public Dimension getMaximumSize(JComponent jc) {
  182.       return this.splitPane != null ? this.layoutManager.maximumLayoutSize(this.splitPane) : new Dimension(0, 0);
  183.    }
  184.  
  185.    public int getMinimumDividerLocation() {
  186.       int minLoc = 0;
  187.       Component leftC = this.splitPane.getLeftComponent();
  188.       if (leftC != null) {
  189.          Insets insets = this.splitPane.getInsets();
  190.          Dimension minSize = leftC.getMinimumSize();
  191.          if (this.orientation == 1) {
  192.             minLoc = minSize.width;
  193.          } else {
  194.             minLoc = minSize.height;
  195.          }
  196.  
  197.          minLoc += this.getDividerBorderSize();
  198.          if (insets != null) {
  199.             if (this.orientation == 1) {
  200.                minLoc += insets.left;
  201.             } else {
  202.                minLoc += insets.top;
  203.             }
  204.          }
  205.       }
  206.  
  207.       return minLoc;
  208.    }
  209.  
  210.    public Dimension getMinimumSize(JComponent jc) {
  211.       return this.splitPane != null ? this.layoutManager.minimumLayoutSize(this.splitPane) : new Dimension(0, 0);
  212.    }
  213.  
  214.    public Component getNonContinuousLayoutDivider() {
  215.       return this.nonContinuousLayoutDivider;
  216.    }
  217.  
  218.    public int getOrientation() {
  219.       return this.orientation;
  220.    }
  221.  
  222.    public Dimension getPreferredSize(JComponent jc) {
  223.       return this.splitPane != null ? this.layoutManager.preferredLayoutSize(this.splitPane) : new Dimension(0, 0);
  224.    }
  225.  
  226.    public JSplitPane getSplitPane() {
  227.       return this.splitPane;
  228.    }
  229.  
  230.    protected void installDefaults(JComponent c) {
  231.       this.splitPane = (JSplitPane)c;
  232.       LookAndFeel.installBorder(c, "SplitPane.border");
  233.       if (this.divider == null) {
  234.          this.divider = this.createDefaultDivider();
  235.       }
  236.  
  237.       this.divider.setBasicSplitPaneUI(this);
  238.       this.splitPane.setDividerSize((Integer)UIManager.get("SplitPane.dividerSize"));
  239.       this.divider.setDividerSize(this.splitPane.getDividerSize());
  240.       this.splitPane.add(this.divider, "divider");
  241.       this.setOrientation(this.splitPane.getOrientation());
  242.       this.setContinuousLayout(this.splitPane.isContinuousLayout());
  243.       this.resetLayoutManager();
  244.       if (this.nonContinuousLayoutDivider == null) {
  245.          this.setNonContinuousLayoutDivider(this.createDefaultNonContinuousLayoutDivider(), true);
  246.       } else {
  247.          this.setNonContinuousLayoutDivider(this.nonContinuousLayoutDivider, true);
  248.       }
  249.  
  250.    }
  251.  
  252.    protected void installListeners(JComponent c) {
  253.       this.splitPane.addPropertyChangeListener(this);
  254.    }
  255.  
  256.    public void installUI(JComponent c) {
  257.       this.installDefaults(c);
  258.       this.installListeners(c);
  259.       this.splitPane.layout();
  260.    }
  261.  
  262.    public boolean isContinuousLayout() {
  263.       return this.continuousLayout;
  264.    }
  265.  
  266.    public void paint(Graphics g, JComponent jc) {
  267.    }
  268.  
  269.    public void propertyChange(PropertyChangeEvent e) {
  270.       if (((EventObject)e).getSource() == this.splitPane) {
  271.          String changeName = e.getPropertyName();
  272.          if (changeName.equals("orientation")) {
  273.             this.orientation = this.splitPane.getOrientation();
  274.             this.resetLayoutManager();
  275.          } else if (changeName.equals("continuousLayout")) {
  276.             this.setContinuousLayout(this.splitPane.isContinuousLayout());
  277.             if (!this.isContinuousLayout()) {
  278.                if (this.nonContinuousLayoutDivider == null) {
  279.                   this.setNonContinuousLayoutDivider(this.createDefaultNonContinuousLayoutDivider(), true);
  280.                } else if (this.nonContinuousLayoutDivider.getParent() == null) {
  281.                   this.setNonContinuousLayoutDivider(this.nonContinuousLayoutDivider, true);
  282.                }
  283.             }
  284.          } else if (changeName.equals("dividerSize")) {
  285.             this.divider.setDividerSize(this.splitPane.getDividerSize());
  286.             this.layoutManager.resetSizeAt(2);
  287.             this.splitPane.invalidate();
  288.             this.splitPane.validate();
  289.          }
  290.       }
  291.  
  292.    }
  293.  
  294.    protected void resetLayoutManager() {
  295.       if (this.orientation == 1) {
  296.          this.layoutManager = new BasicHorizontalLayoutManager(this);
  297.       } else {
  298.          this.layoutManager = new BasicVerticalLayoutManager(this);
  299.       }
  300.  
  301.       this.splitPane.setLayout(this.layoutManager);
  302.       this.layoutManager.updateComponents();
  303.       this.splitPane.validate();
  304.       this.splitPane.repaint();
  305.    }
  306.  
  307.    public void resetToPreferredSizes() {
  308.       if (this.splitPane != null) {
  309.          this.layoutManager.resetToPreferredSizes();
  310.          this.splitPane.validate();
  311.          this.layoutManager.layoutContainer(this.splitPane);
  312.       }
  313.  
  314.    }
  315.  
  316.    public void setContinuousLayout(boolean b) {
  317.       this.continuousLayout = b;
  318.    }
  319.  
  320.    public void setDividerLocation(int location) {
  321.       Component leftC = this.splitPane.getLeftComponent();
  322.       Component rightC = this.splitPane.getRightComponent();
  323.       if (leftC != null && rightC != null) {
  324.          Insets insets = this.splitPane.getInsets();
  325.          if (insets != null) {
  326.             if (this.orientation == 1) {
  327.                leftC.setSize(Math.max(0, location - insets.left), 10);
  328.             } else {
  329.                leftC.setSize(10, Math.max(0, location - insets.top));
  330.             }
  331.          } else if (this.orientation == 1) {
  332.             leftC.setSize(Math.max(0, location), 10);
  333.          } else {
  334.             leftC.setSize(10, Math.max(0, location));
  335.          }
  336.  
  337.          this.splitPane.validate();
  338.          this.splitPane.repaint();
  339.       }
  340.  
  341.    }
  342.  
  343.    public void setLastDragLocation(int l) {
  344.       this.lastDragLocation = l;
  345.    }
  346.  
  347.    protected void setNonContinuousLayoutDivider(Component newDivider) {
  348.       this.setNonContinuousLayoutDivider(newDivider, true);
  349.    }
  350.  
  351.    protected void setNonContinuousLayoutDivider(Component newDivider, boolean rememberSizes) {
  352.       if (this.nonContinuousLayoutDivider != null && this.splitPane != null) {
  353.          this.splitPane.remove(this.nonContinuousLayoutDivider);
  354.       }
  355.  
  356.       this.nonContinuousLayoutDivider = newDivider;
  357.       if (this.nonContinuousLayoutDivider != null && this.splitPane != null) {
  358.          this.nonContinuousLayoutDivider.setLocation(-1000, -1000);
  359.          Component leftC = this.splitPane.getLeftComponent();
  360.          Component rightC = this.splitPane.getRightComponent();
  361.          int[] sizes = this.layoutManager.getSizes();
  362.          if (leftC != null) {
  363.             this.splitPane.setLeftComponent((Component)null);
  364.          }
  365.  
  366.          if (rightC != null) {
  367.             this.splitPane.setRightComponent((Component)null);
  368.          }
  369.  
  370.          this.splitPane.remove(this.divider);
  371.          this.splitPane.add(this.nonContinuousLayoutDivider, "nonContinuousDivider", this.splitPane.getComponentCount());
  372.          this.splitPane.setLeftComponent(leftC);
  373.          this.splitPane.setRightComponent(rightC);
  374.          this.splitPane.add(this.divider, "divider");
  375.          if (rememberSizes) {
  376.             this.layoutManager.setSizes(sizes);
  377.          }
  378.  
  379.          this.splitPane.validate();
  380.          this.splitPane.paintImmediately(this.splitPane.getX(), this.splitPane.getY(), this.splitPane.getWidth(), this.splitPane.getHeight());
  381.       }
  382.  
  383.    }
  384.  
  385.    public void setOrientation(int orientation) {
  386.       this.orientation = orientation;
  387.    }
  388.  
  389.    protected void startDragging() {
  390.       Component leftC = this.splitPane.getLeftComponent();
  391.       Component rightC = this.splitPane.getRightComponent();
  392.       this.beginDragDividerLocation = this.getDividerLocation();
  393.       this.draggingHW = false;
  394.       ComponentPeer cPeer;
  395.       if (leftC != null && (cPeer = leftC.getPeer()) != null && !(cPeer instanceof LightweightPeer)) {
  396.          this.draggingHW = true;
  397.       } else if (rightC != null && (cPeer = rightC.getPeer()) != null && !(cPeer instanceof LightweightPeer)) {
  398.          this.draggingHW = true;
  399.       }
  400.  
  401.       if (this.orientation == 1) {
  402.          this.setLastDragLocation(this.divider.getBounds().x);
  403.          this.dividerSize = this.divider.getSize().width + 2 * this.getDividerBorderSize();
  404.          if (!this.isContinuousLayout() && this.draggingHW) {
  405.             this.nonContinuousLayoutDivider.setBounds(this.getLastDragLocation(), 0, this.dividerSize, this.splitPane.getHeight());
  406.          }
  407.       } else {
  408.          this.setLastDragLocation(this.divider.getBounds().y);
  409.          this.dividerSize = this.divider.getSize().height + 2 * this.getDividerBorderSize();
  410.          if (!this.isContinuousLayout() && this.draggingHW) {
  411.             this.nonContinuousLayoutDivider.setBounds(0, this.getLastDragLocation(), this.splitPane.getWidth(), this.dividerSize);
  412.          }
  413.       }
  414.  
  415.    }
  416.  
  417.    protected void uninstallDefaults(JComponent c) {
  418.       if (this.splitPane.getLayout() == this.layoutManager) {
  419.          this.splitPane.setLayout((LayoutManager)null);
  420.       }
  421.  
  422.       if (this.nonContinuousLayoutDivider != null) {
  423.          this.splitPane.remove(this.nonContinuousLayoutDivider);
  424.       }
  425.  
  426.       this.splitPane.remove(this.divider);
  427.       this.divider.setBasicSplitPaneUI((BasicSplitPaneUI)null);
  428.       this.splitPane = null;
  429.       this.layoutManager = null;
  430.       this.setNonContinuousLayoutDivider((Component)null);
  431.    }
  432.  
  433.    protected void uninstallListeners(JComponent c) {
  434.       this.splitPane.removePropertyChangeListener(this);
  435.    }
  436.  
  437.    public void uninstallUI(JComponent c) {
  438.       this.uninstallListeners(c);
  439.       this.uninstallDefaults(c);
  440.    }
  441.  
  442.    static int access$orientation(BasicSplitPaneUI var0) {
  443.       return var0.orientation;
  444.    }
  445. }
  446.